﻿#region License

// // -------------------------------------------------------------------------------------
// // This information refers to PerformanceInstrumentationService.cs
// // 
// // Copyright 2012 ELEN7046 Group 2
// // 
// // Last Modified on: 2012-06-10
// // 
// // Contributors:
// // 
// // Amith Sewnarain
// // Elmar Vosloo
// // Jason Thorne
// // Satish Jewan
// // 
// // Wits University 
// // School of Electrical & Information Engineering
// // Software Engineering Tools and Techniques
// // 
// // -------------------------------------------------------------------------------------
// // 
// // This file (PerformanceInstrumentationService.cs) is part of the DishDash Event Visualization System.
// // 
// // DishDash is free software: you can redistribute it and/or modify
// // it under the terms of the GNU General Public License as published by
// // the Free Software Foundation, either version 3 of the License, or
// // (at your option) any later version.
// // 
// // DishDash is distributed in the hope that it will be useful,
// // but WITHOUT ANY WARRANTY; without even the implied warranty of
// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// // GNU General Public License for more details.
// // 
// // You should have received a copy of the GNU General Public License
// // along with DishDash.  
// // If not, see <http://www.gnu.org/licenses/>.
// // -------------------------------------------------------------------------------------

#endregion

#region Using Directives

using System;
using System.Configuration;
using DishDash.Common;
using RabbitMQIntegration;

#endregion

namespace MessageAPI.Core.Implementation
{
    public class PerformanceInstrumentationService : IPerformanceInstrumentationService
    {
        #region IPerformanceInstrumentationService Members

        /// <summary>
        ///   Sends the event.
        /// </summary>
        /// <param name="performanceEvent"> The performance event. </param>
        public void SendEvent(PerformanceEvent performanceEvent)
        {
            IDistributedCommunicationHandler distributedCommunicationHandler;
            try
            {
                var enrichedPerformanceEvent = this.EnrichPerformanceEvent(performanceEvent);
                distributedCommunicationHandler = new RabbitMQHandler();
                distributedCommunicationHandler.SendMessage(performanceEvent.Category,
                                                            enrichedPerformanceEvent.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                throw;
            }
        }

        #endregion

        /// <summary>
        ///   Enriches the performance event.
        /// </summary>
        /// <param name="performanceEvent"> The performance event. </param>
        /// <returns> </returns>
        private PerformanceEvent EnrichPerformanceEvent(PerformanceEvent performanceEvent)
        {
            string machineName = string.Empty;

            var configuredMachineOverride = ConfigurationManager.AppSettings.Get("MachineNameOverride");

            if (configuredMachineOverride != null && configuredMachineOverride != string.Empty)
            {
                machineName = configuredMachineOverride;
            }
            else
            {
                machineName = Environment.MachineName;
            }

            performanceEvent.AddContextValue("Host", machineName);

            return performanceEvent;
        }
    }
}